<?php
namespace Admin\Controller;

use Utuo\BackgroundController;

class UserSaleController extends BackgroundController {
    
    public function _initialize() {
        $this->setControllerTitle("金牌会员统计");
        $this->setActionTitle(array(
//             'index' => "金牌会员统计",
//             'showUserSales' => '查看统计结果',
            'userDealer' => '发展金牌会员统计'
        ));
        $this->setActionNav(array("userDealer"));
    }
    
    public function indexAction() {
        $cid = I('cid', 0, 'intval');
        $startTime = I('stime', 0, 'strtotime');
        $endTime = I('etime', 0, 'strtotime') or $endTime = time();
        $this->assign("startTime", $startTime);
        $this->assign("endTime", $endTime);
        $this->assign("cid", $cid);
        $this->display();
    }
    
    public function indexPostAction() {
        $cid = I('cid', 0, 'intval');
        M("UserSales")->where("id is not null")->delete();
        $list = M("WechatUser")->where(array('cid'=>$cid))->getField('id', true);
        if ( $list ) {
            $this->ajaxSuccess("操作成功", $list);
        }
        $this->ajaxError("操作失败");
    }
    
    public function ajaxUserSalesAction() {
        $id = I('id', 0, 'intval');
        $startTime = I('stime', 0, 'strtotime');
        $endTime = I('etime', 0, 'strtotime') or $endTime = strtotime("-30 days");
        $sales = M("WechatUser u")->join("__DISTRIBUTION_SHOP_ORDER__ o on u.id=o.uid")->where(array(
            'u.mid'=>$id,
            'u.createTime'=>array('between', array($startTime, $endTime)),
            'u.type' => array('exp', "is not null and SUBSTRING(bin(u.type),-3,1)*1>0"),
            'o.pay_status' => 1
        ))->field("count(distinct u.id) as count, sum(o.actual_cost) as sale")->find();
        if ( false !== $sales ) {
            if ( false !== M("UserSales")->add(array(
                'uid' => $id,
                'count' => $sales['count']*1,
                'sale' => $sales['sale']*1,
            )) ) {
                $this->ajaxSuccess();
            }
        }
        $this->ajaxError();
    }
     
    public function showUserSalesAction() {
        $list = M("UserSales a")->join("__WECHAT_USER__ b on a.uid=b.id")->field("a.*,b.nickname,b.openId")->order("a.count desc")->limit(30)->select();
        $this->assign("list", $list);
        $this->display();
    }    
    
    function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
        if(is_array($arrays)){
            foreach ($arrays as $array){
                if(is_array($array)){
                    $key_arrays[] = $array[$sort_key];
                }else{
                    return false;
                }
            }
        }else{
            return false;
        }
        array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
        return $arrays;
    }
    
    /**
     * 用户金牌会员发展数
     */
    public function userDealerAction() {
        $cid = I('cid', 0, 'intval');
        $startTime = I('stime', 0, 'strtotime');
        $endTime = I('etime', 0, 'strtotime') or $endTime = time();
        $dlist = M("Dealer d")->join("__WECHAT_USER__ u on d.wechat_uid=u.id")->join("__WECHAT_USER__ u1 on u.mid=u1.id")->where(array(
            'd.cid' => $cid,
            'd.begin_time'=>array('between', array($startTime, $endTime))
        ))->field("d.name as dealer_name,d.cid,u1.id,u1.openId,u1.nickname")->order("u.mid")->select();
        //echo M()->getLastSql();
        $list = array();
        foreach( $dlist as &$v ) {
            if ( $v['id'] ) {
                $list[$v['id']]['id'] = $v['id'];
                $list[$v['id']]['count'] += 1;
                $list[$v['id']]['cid'] = $v['cid'];
                $list[$v['id']]['nickname'] = $v['nickname'];
                $list[$v['id']]['openId'] = $v['openId'];
            }
        }
        $list = $this->my_sort($list, 'count', SORT_DESC, SORT_NUMERIC);  
        
        $this->assign("list", $list);
        $this->assign("startTime", $startTime);
        $this->assign("endTime", $endTime);
        $this->assign("cid", $cid);
        $this->display();
    }
}